package com.baobao.dao;

import com.baobao.domain.School;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.skyway.spring.util.dao.AbstractJpaDao;

import org.springframework.dao.DataAccessException;

import org.springframework.stereotype.Repository;

import org.springframework.transaction.annotation.Transactional;

/**
 * DAO to manage School entities.
 * 
 */
@Repository("SchoolDAO")
@Transactional
public class SchoolDAOImpl extends AbstractJpaDao<School> implements SchoolDAO {

	/**
	 * Set of entity classes managed by this DAO.  Typically a DAO manages a single entity.
	 *
	 */
	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(Arrays.asList(new Class<?>[] { School.class }));

	/**
	 * EntityManager injected by Spring for persistence unit BaoBao
	 *
	 */
	@PersistenceContext(unitName = "BaoBao")
	private EntityManager entityManager;

	/**
	 * Instantiates a new SchoolDAOImpl
	 *
	 */
	public SchoolDAOImpl() {
		super();
	}

	/**
	 * Get the entity manager that manages persistence unit 
	 *
	 */
	public EntityManager getEntityManager() {
		return entityManager;
	}

	/**
	 * Returns the set of entity classes managed by this DAO.
	 *
	 */
	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	/**
	 * JPQL Query - findSchoolByNameContaining
	 *
	 */
	@Transactional
	public Set<School> findSchoolByNameContaining(String name) throws DataAccessException {

		return findSchoolByNameContaining(name, -1, -1);
	}

	/**
	 * JPQL Query - findSchoolByNameContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<School> findSchoolByNameContaining(String name, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findSchoolByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<School>(query.getResultList());
	}

	/**
	 * JPQL Query - findSchoolByPhoneContaining
	 *
	 */
	@Transactional
	public Set<School> findSchoolByPhoneContaining(String phone) throws DataAccessException {

		return findSchoolByPhoneContaining(phone, -1, -1);
	}

	/**
	 * JPQL Query - findSchoolByPhoneContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<School> findSchoolByPhoneContaining(String phone, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findSchoolByPhoneContaining", startResult, maxRows, phone);
		return new LinkedHashSet<School>(query.getResultList());
	}

	/**
	 * JPQL Query - findSchoolByPrimaryKey
	 *
	 */
	@Transactional
	public School findSchoolByPrimaryKey(String id) throws DataAccessException {

		return findSchoolByPrimaryKey(id, -1, -1);
	}

	/**
	 * JPQL Query - findSchoolByPrimaryKey
	 *
	 */

	@Transactional
	public School findSchoolByPrimaryKey(String id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findSchoolByPrimaryKey", startResult, maxRows, id);
			return (com.baobao.domain.School) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	/**
	 * JPQL Query - findSchoolByCity
	 *
	 */
	@Transactional
	public Set<School> findSchoolByCity(String city) throws DataAccessException {

		return findSchoolByCity(city, -1, -1);
	}

	/**
	 * JPQL Query - findSchoolByCity
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<School> findSchoolByCity(String city, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findSchoolByCity", startResult, maxRows, city);
		return new LinkedHashSet<School>(query.getResultList());
	}

	/**
	 * JPQL Query - findSchoolByPhone
	 *
	 */
	@Transactional
	public Set<School> findSchoolByPhone(String phone) throws DataAccessException {

		return findSchoolByPhone(phone, -1, -1);
	}

	/**
	 * JPQL Query - findSchoolByPhone
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<School> findSchoolByPhone(String phone, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findSchoolByPhone", startResult, maxRows, phone);
		return new LinkedHashSet<School>(query.getResultList());
	}

	/**
	 * JPQL Query - findSchoolByPostcodeContaining
	 *
	 */
	@Transactional
	public Set<School> findSchoolByPostcodeContaining(String postcode) throws DataAccessException {

		return findSchoolByPostcodeContaining(postcode, -1, -1);
	}

	/**
	 * JPQL Query - findSchoolByPostcodeContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<School> findSchoolByPostcodeContaining(String postcode, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findSchoolByPostcodeContaining", startResult, maxRows, postcode);
		return new LinkedHashSet<School>(query.getResultList());
	}

	/**
	 * JPQL Query - findSchoolByPostcode
	 *
	 */
	@Transactional
	public Set<School> findSchoolByPostcode(String postcode) throws DataAccessException {

		return findSchoolByPostcode(postcode, -1, -1);
	}

	/**
	 * JPQL Query - findSchoolByPostcode
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<School> findSchoolByPostcode(String postcode, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findSchoolByPostcode", startResult, maxRows, postcode);
		return new LinkedHashSet<School>(query.getResultList());
	}

	/**
	 * JPQL Query - findSchoolByCounty
	 *
	 */
	@Transactional
	public Set<School> findSchoolByCounty(String county) throws DataAccessException {

		return findSchoolByCounty(county, -1, -1);
	}

	/**
	 * JPQL Query - findSchoolByCounty
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<School> findSchoolByCounty(String county, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findSchoolByCounty", startResult, maxRows, county);
		return new LinkedHashSet<School>(query.getResultList());
	}

	/**
	 * JPQL Query - findSchoolByProvince
	 *
	 */
	@Transactional
	public Set<School> findSchoolByProvince(String province) throws DataAccessException {

		return findSchoolByProvince(province, -1, -1);
	}

	/**
	 * JPQL Query - findSchoolByProvince
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<School> findSchoolByProvince(String province, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findSchoolByProvince", startResult, maxRows, province);
		return new LinkedHashSet<School>(query.getResultList());
	}

	/**
	 * JPQL Query - findSchoolByCityContaining
	 *
	 */
	@Transactional
	public Set<School> findSchoolByCityContaining(String city) throws DataAccessException {

		return findSchoolByCityContaining(city, -1, -1);
	}

	/**
	 * JPQL Query - findSchoolByCityContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<School> findSchoolByCityContaining(String city, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findSchoolByCityContaining", startResult, maxRows, city);
		return new LinkedHashSet<School>(query.getResultList());
	}

	/**
	 * JPQL Query - findSchoolByIdContaining
	 *
	 */
	@Transactional
	public Set<School> findSchoolByIdContaining(String id) throws DataAccessException {

		return findSchoolByIdContaining(id, -1, -1);
	}

	/**
	 * JPQL Query - findSchoolByIdContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<School> findSchoolByIdContaining(String id, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findSchoolByIdContaining", startResult, maxRows, id);
		return new LinkedHashSet<School>(query.getResultList());
	}

	/**
	 * JPQL Query - findSchoolByAddress
	 *
	 */
	@Transactional
	public Set<School> findSchoolByAddress(String address) throws DataAccessException {

		return findSchoolByAddress(address, -1, -1);
	}

	/**
	 * JPQL Query - findSchoolByAddress
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<School> findSchoolByAddress(String address, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findSchoolByAddress", startResult, maxRows, address);
		return new LinkedHashSet<School>(query.getResultList());
	}

	/**
	 * JPQL Query - findSchoolById
	 *
	 */
	@Transactional
	public School findSchoolById(String id) throws DataAccessException {

		return findSchoolById(id, -1, -1);
	}

	/**
	 * JPQL Query - findSchoolById
	 *
	 */

	@Transactional
	public School findSchoolById(String id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findSchoolById", startResult, maxRows, id);
			return (com.baobao.domain.School) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	/**
	 * JPQL Query - findAllSchools
	 *
	 */
	@Transactional
	public Set<School> findAllSchools() throws DataAccessException {

		return findAllSchools(-1, -1);
	}

	/**
	 * JPQL Query - findAllSchools
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<School> findAllSchools(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllSchools", startResult, maxRows);
		return new LinkedHashSet<School>(query.getResultList());
	}

	/**
	 * JPQL Query - findSchoolByName
	 *
	 */
	@Transactional
	public Set<School> findSchoolByName(String name) throws DataAccessException {

		return findSchoolByName(name, -1, -1);
	}

	/**
	 * JPQL Query - findSchoolByName
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<School> findSchoolByName(String name, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findSchoolByName", startResult, maxRows, name);
		return new LinkedHashSet<School>(query.getResultList());
	}

	/**
	 * JPQL Query - findSchoolByCountyContaining
	 *
	 */
	@Transactional
	public Set<School> findSchoolByCountyContaining(String county) throws DataAccessException {

		return findSchoolByCountyContaining(county, -1, -1);
	}

	/**
	 * JPQL Query - findSchoolByCountyContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<School> findSchoolByCountyContaining(String county, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findSchoolByCountyContaining", startResult, maxRows, county);
		return new LinkedHashSet<School>(query.getResultList());
	}

	/**
	 * JPQL Query - findSchoolByAddressContaining
	 *
	 */
	@Transactional
	public Set<School> findSchoolByAddressContaining(String address) throws DataAccessException {

		return findSchoolByAddressContaining(address, -1, -1);
	}

	/**
	 * JPQL Query - findSchoolByAddressContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<School> findSchoolByAddressContaining(String address, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findSchoolByAddressContaining", startResult, maxRows, address);
		return new LinkedHashSet<School>(query.getResultList());
	}

	/**
	 * JPQL Query - findSchoolByProvinceContaining
	 *
	 */
	@Transactional
	public Set<School> findSchoolByProvinceContaining(String province) throws DataAccessException {

		return findSchoolByProvinceContaining(province, -1, -1);
	}

	/**
	 * JPQL Query - findSchoolByProvinceContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<School> findSchoolByProvinceContaining(String province, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findSchoolByProvinceContaining", startResult, maxRows, province);
		return new LinkedHashSet<School>(query.getResultList());
	}

	/**
	 * Used to determine whether or not to merge the entity or persist the entity when calling Store
	 * @see store
	 * 
	 *
	 */
	public boolean canBeMerged(School entity) {
		return true;
	}
}
